<?php 
header("content-type:text/html;charset=utf8");
$goods_name = isset($_POST['goods_name'])?$_POST['goods_name']:'';
$redis = new Redis();$redis->connect('127.0.0.1','6379');
$pdo = new PDO("mysql:host=127.0.0.1;dbname=liu;charset=utf8",'root','root');
if ($redis->get('goods_name')==$goods_name) {
	for ($i=1; $i <$redis->get('i') ; $i++) { 
		$data[] = $redis->hgetall('goods_'.$i);
	}
	//print_r($data);
	echo "redis查的";
}
else
{
	$data = $pdo->query("select * from goods where goods_name like '%$goods_name%'")->fetchall(PDO::FETCH_ASSOC);
	//print_r($data);
	$i=1;
	foreach ($data as $key => $value) {
		$redis->hmset('goods_'.$i,$value);$i++;
	}
	$redis->set('i',$i);
	$redis->set('goods_name',$data[0]['goods_name']);
}











 ?>
  <!doctype html>
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
 	<title>Document</title>
 </head>
 <body>
 	<center>
 		<table>
 			<form action="search.php" method="post">
 				商品名:<input type="text" name="goods_name"><input type="submit" value="搜索">
 			</form>
 			<tr>
 				<th>商品名</th>
 				<th>商品分类</th>
 				<th>品牌</th>
 				<th>价格</th>
 				<th>库存</th>
 				<th>操作</th>

 			</tr>
 			<?php foreach ($data as $key => $value): ?>
 			   <tr>
 			   	  <td><?php echo $value['goods_name'] ?></td>
 			   	  <td><?php echo $value['goods_class'] ?></td>
 			   	  <td><?php echo $value['goods_grand'] ?></td>
 			   	  <td><?php echo $value['goods_price'] ?></td>
 			   	  <td><?php echo $value['goods_stock'] ?></td>
 			   	  <td><a href="detail.php?id=<?php echo $value['id'] ?>"><button>查看详情</button></a></td>

 			   </tr>
 			<?php endforeach ?>
 		</table>
 		
 	</center>
 </body>
 </html>